﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
//using System.Windows.Forms;
using System.Timers;

namespace VirtualVisionSpace
{
    class VirtualSpace
    {
        public delegate void SpaceStreamCallback(Bitmap stream);
        SpaceStreamCallback spaceStreamCall;
        Bitmap imageSpace = new Bitmap(704, 576);
        Timer timer = new Timer();
        Bitmap imageObject = new Bitmap(100, 100);
        Point pointObject = new Point(300, 200);
        Bitmap imageBase;
        static Random random = new Random();

        public VirtualSpace()
        {
            this.timer.Interval = 40;
            //this.timer.Tick += new EventHandler(timer_Tick);
            this.timer.Elapsed += new ElapsedEventHandler(timer_Tick);
            GetRandomImage(this.imageObject);
            GetRandomImage(this.imageSpace);
            imageBase = this.imageSpace.Clone() as Bitmap;
        }

        //if (data.PixelFormat == PixelFormat.Format32bppArgb)
        //{//为4通道B G R A，A默认为0
        //    int index = (h * space.Width + w) * 4;
        //    pData[index] = 0;
        //    pData[index + 1] = 0;
        //    pData[index + 2] = 255;
        //    pData[index + 3] = 255;
        //}
        //else if (data.PixelFormat == PixelFormat.Format24bppRgb)
        //{//为3通道B G R, A默认为255
        //}
        int counter = 0;
        void timer_Tick(object sender, EventArgs e)
        {
            if (this.spaceStreamCall != null)
            {
                GetRandomImage(this.imageSpace);
                if (++counter % 1 == 0)
                {
                    //GetRandomImage(this.imageSpace);
                }
                //GetRandomImage(this.imageObject);
                if (counter % 3 == 0)
                {
                    GetRandomImage(this.imageObject);
                }

                lock (this.imageObject)
                {
                    Graphics G = Graphics.FromImage(this.imageSpace);
                    //G.DrawImage(imageBase, 0, 0);//背景保持
                    G.DrawImage(this.imageObject,
                        this.pointObject.X,
                        this.pointObject.Y);
                    //G.DrawRectangle(Pens.Red, new Rectangle(this.pointObject, this.imageObject.Size));
                    this.pointObject.Offset(3, 3);
                    this.pointObject.X %= this.imageSpace.Width;
                    this.pointObject.Y %= this.imageSpace.Height;
                }

                if (this.spaceStreamCall != null)
                    this.spaceStreamCall(this.imageSpace);
            }
        }

        //获取随机图像
        public static void GetRandomImage(Bitmap image)
        {
            lock (image)
            {
                BitmapData data = image.LockBits(
                    new Rectangle(0, 0, image.Width, image.Height),
                    ImageLockMode.WriteOnly,
                    PixelFormat.Format24bppRgb);

                unsafe
                {
                    byte* pData = (byte*)data.Scan0.ToPointer();

                    for (int h = 0; h < image.Height; h++)
                    {
                        for (int w = 0; w < image.Width; w++)
                        {
                            int index = (h * image.Width + w) * 3;

                            pData[index] = //(byte)random.Next(256);
                            pData[index + 1] = //(byte)random.Next(256);
                            pData[index + 2] = (byte)random.Next(256);
                        }
                    }
                }

                image.UnlockBits(data);
            }
        }

        public void Open(SpaceStreamCallback spaceStreamCall)
        {
            this.spaceStreamCall = spaceStreamCall;
            this.timer.Start();
        }

        public void Close()
        {
            this.timer.Stop();
            this.spaceStreamCall = null;
        }
    }
}
